/* global window */
import modelExtend from 'dva-model-extend'
import * as operatorService from '../../services/operator'
import {pageModel} from 'utils/model'
import pathToRegexp from 'path-to-regexp'
import * as employeeService from "../../../employee/services/employees"
import * as dictionaryService from 'services/dictionary'
import {message} from 'antd'


const {findByWxId, update, queryPassword} = operatorService

export default modelExtend(pageModel, {
  namespace: 'operatorDetail',

  state: {
    disabled: true,
    hideViewInfoBtn: false,
    empList: [],
    operateStatus: [],
    figureIp: [],
    friendCircleType: [],
    source: [],
    data: {},
    modelVisible: false,
  },

  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({pathname}) => {
        const match = pathToRegexp('/operator/:id').exec(pathname)
        if (match) {
          dispatch({type: 'operatorDetail/query', payload: {wxId: match[1]}})
          dispatch({type: 'customer/query', payload: {belongWxId: match[1], current: 1, size: 10}})
          dispatch({type: 'init'})
          dispatch({type: 'resetHideViewInfoBtn'})
          dispatch({type: 'searchEmp', payload: {key: 'any'}})
        }
      })
    },
  },

  effects: {

    * query ({payload = {}}, {call, put}) {
      const response = yield call(findByWxId, payload)
      const {data} = response
      if (response && response.code === 0 && data) {
        yield put({type: 'updateState', payload: {data}})
      } else {
        throw data
      }
    },

    * init ({payload = {}}, {call, put}) {
      const figureIpResp = yield call(dictionaryService.queryBySn, {sn: 'figure_ip'})
      const friendCircleTypeResp = yield call(dictionaryService.queryBySn, {sn: 'friend_circle_type'})
      const sourceResp = yield call(dictionaryService.queryBySn, {sn: 'source'})
      const operateStatusResp = yield call(dictionaryService.queryBySn, {sn: 'operate_status'})
      yield put({
        type: 'updateState', payload: {
          figureIp: figureIpResp.data,
          friendCircleType: friendCircleTypeResp.data,
          source: sourceResp.data,
          operateStatus: operateStatusResp.data,
        },
      })
    },

    * searchEmp ({payload = {}}, {call, put}) {
      const response = yield call(employeeService.searchEmp, payload)
      const {data} = response
      if (response && data) {
        yield put({
          type: 'updateState',
          payload: {
            empList: data,
          },
        })
      }
    },

    * update ({payload}, {call, put}) {
      const {data, code, msg} = yield call(update, payload)
      if (code === 0) {
        message.success(msg)
      } else {
        message.error(msg)
        throw data
      }
    },

    * queryPassword ({payload}, {call, put}) {
      const response = yield call(queryPassword, payload)
      const {data, code, msg} = response
      if (code === 0) {
        message.success(msg)
        let keys = Object.keys(data)
        yield put({
          type: 'updateDate',
          payload: {
            data,
          },
        })
        yield put({
          type: 'hideModal',
        })
      } else {
        message.error(msg)
      }
    },
  },

  reducers: {
    getDetail (state, {payload}) {
      return {...state, ...payload}
    },

    updateDate (state, {payload}) {
      const {data} = state
      const {data: dataResp} = payload
      let {wxLoginPassword, payPassword} = dataResp
      data.loginPassword = wxLoginPassword
      data.pay.payPassword = payPassword
      return {...state, hideViewInfoBtn: true, data}
    },

    showModal (state, {payload}) {
      return {...state, modelVisible: true}
    },


    hideModal (state) {
      return {...state, modelVisible: false}
    },

    resetHideViewInfoBtn (state) {
      return {...state, hideViewInfoBtn: false}
    },
  },
})
